package com.lsyw.intergrate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsyw.intergrate.common.Result;
import com.lsyw.intergrate.entity.SysNo;
import com.lsyw.intergrate.entity.common.PageInfo;
import com.lsyw.intergrate.service.SysNoService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 系统编号 前端控制器
 * </p>
 *
 * @author 刘磊
 * @since 2023-05-11
 */
@Api(tags = "系统编号")
@RestController
@RequestMapping("/sys-no")
public class SysNoController {


    @Autowired
    private SysNoService sysNoService;


    /**
     * 通过id查询
     */
    @GetMapping("/get-by-id")
    public Result getById(String id) {
        return Result.successResult(sysNoService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    public Result save(@RequestBody SysNo sysNo) {
        sysNoService.save(sysNo);
        return Result.successResult(null);
    }

    /**
     * 通过id删除
     */
    @GetMapping("/delete-by-id/{id}")
    public Result delete(@PathVariable(value = "id") String ids) {
        String[] idsStrs = ids.split(",");
        for (String id : idsStrs) {
            sysNoService.removeById(Long.parseLong(id));
        }
        return Result.successResult(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result updateById(@RequestBody SysNo sysNo) {
        sysNoService.updateById(sysNo);
        return Result.successResult(null);
    }


    /**
     * 查询列表
     */
    @PostMapping("/list")
    public Result list(@RequestBody SysNoReqVo sysNo) {
        final LambdaQueryWrapper<SysNo> lambda = new QueryWrapper<SysNo>().lambda();
        this.buildCondition(lambda, sysNo);
        return Result.successResult(sysNoService.list(lambda));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public Result page(@RequestBody PageInfo pageInfo) {
        Page<SysNo> page = new Page<>();
        page.setSize(pageInfo.getPageSize());
        page.setCurrent(pageInfo.getPageNum());
        final LambdaQueryWrapper<SysNo> lambda = new QueryWrapper<SysNo>().lambda();
        //TODO 自己的查询逻辑
        return Result.successResult(this.sysNoService.page(page, lambda));
    }


    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<SysNo> lambda, SysNoReqVo param) {
        if (!StringUtils.isEmpty(param.getId())) {
            lambda.eq(SysNo::getId, param.getId());
        }
        if (!StringUtils.isEmpty(param.getNo())) {
            lambda.eq(SysNo::getNo, param.getNo());
        }
        if (!StringUtils.isEmpty(param.getDelFlag())) {
            lambda.eq(SysNo::getDelFlag, param.getDelFlag());
        }
        lambda.orderBy(true, false, SysNo::getId);
    }


    /**
     * 请求model
     */
    @Data
    @AllArgsConstructor
    private static class SysNoReqVo extends SysNo {
    }


}
